import { NodeSize } from '../config'

export default function normaliser(flow) {
  flow.register('Normaliser', ({ PolygonNode, PolygonNodeModel, h }) => {
    class Node extends PolygonNode {
      getIconShape() {
        return h(
          'svg',
          {
            width: NodeSize,
            height: NodeSize,
            viewBox: '0 0 1024 1024'
          },
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M77.019429 895.658667h458.069333V437.979429H77.019429v457.679238z m41.447619-41.423238v-106.617905h375.174095v106.617905H118.467048z m0-126.147048v-118.832762h375.174095v118.832762H118.467048z m375.174095-248.685714v110.323809H118.467048v-110.323809h375.174095z'
            }
          ),
          h(
            'path',
            {
              fill: '#0CB20F',
              d: 'M779.215238 410.502095l-32.768 32.768 10.99581 10.99581a84.309333 84.309333 0 0 1 0 119.149714l-78.140953 78.140952v-67.364571h-46.323809v146.456381h146.480762v-46.32381h-67.388953l78.140953-78.140952a130.730667 130.730667 0 0 0 0-184.661333l-10.99581-11.020191z'
            }
          ),
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M736.402286 251.294476h62.512762v41.423238h-62.512762zM494.543238 105.155048h43.690667V146.041905h-43.690667zM562.614857 105.155048h62.488381V146.041905h-62.488381zM649.508571 105.155048h62.512762V146.041905H649.508571zM494.543238 170.398476h41.447619V226.742857h-41.447619zM649.508571 251.294476h62.512762v41.423238H649.508571zM562.614857 251.294476h62.488381v41.423238h-62.488381zM535.990857 251.12381h-41.447619v41.593904h43.690667v-41.423238h-2.243048zM911.164952 170.398476h41.447619V226.742857h-41.447619zM911.164952 251.294476h-0.975238v41.423238h42.422857V251.12381h-41.447619zM823.320381 105.155048h62.488381V146.041905h-62.488381zM823.320381 251.294476h62.488381v41.423238h-62.488381zM910.189714 105.155048h42.422857V146.041905h-42.422857zM736.402286 105.155048h62.512762V146.041905h-62.512762z'
            }
          )
        )
      }

      getLabelShape() {
        return h(
          'text',
          {
            fill: '#000000',
            fontSize: 12,
            x: NodeSize / 2,
            y: NodeSize + 20,
            textAnchor: 'middle',
            dominantBaseline: 'middle'
          },
          '行转列'
        )
      }

      getShape() {
        const { x, y, width, height } = this.getAttributes()
        const transform = `matrix(1 0 0 1 ${x - width / 2} ${y - height / 2})`
        return h(
          'g',
          {
            transform
          },
          [
            h(
              'rect',
              {
                rx: 5,
                ry: 5,
                width: NodeSize,
                height: NodeSize,
                fill: '#fff',
                stroke: '#3D6380',
                strokeWidth: 1
              }
            ),
            this.getIconShape(),
            this.getLabelShape()
          ]
        )
      }
    }
    class Model extends PolygonNodeModel {
      constructor(data, graphModel) {
        super(data, graphModel)
        this.points = [
          [NodeSize / 2, 0],
          [NodeSize, NodeSize / 2],
          [NodeSize / 2, NodeSize],
          [0, NodeSize / 2]
        ]
      }
    }

    return {
      view: Node,
      model: Model
    }
  })
}
